<?php 

if(!defined('DB_NAME')) die();

$description =
array(
	"name" => "description",
	"desc" => "（默认截取前200字符）",
	"type" => "textarea",
	"title" => "文章描述");
$keywords =
array(
	"name" => "keywords",
	"desc" => "（半角逗号隔开，默认使用标签）",
	"type" => "textarea",
	"title" => "关键词");
//页面一级导航菜单
$pgmenu =
array(
	"name" => "pgmenu",
	"type" => "hidden");
$cc =
array(
	"name" => "cc",
	"type" => "select",
	"default" => "BY-NC-SA",
	"title" => "知识产权：",
	"options"=> array( "BY|署名", "BY-ND|署名-禁止演绎", "BY-SA|署名-相同方式共享", "BY-NC|署名-非商业","BY-NC-ND|署名-非商业-禁止演绎", "BY-NC-SA|署名-非商业-相同方式共享")
);
$top =
array(
	"name" => "top",
	"type" => "checkbox",
	"title" => "加入幻灯片：");
$thumb =
array(
	"name" => "thumb",
	"desc" => "（作为特色图像，430x230）",
	"type" => "text",
	"size" => "108",
	"title" => "缩略图地址");
//开启媒体分享后添加的栏目
if ( get('ini_media') ):
	$media_music =
	array(
		"name" => "music",
		"type" => "text",
		"size" => "100",
		"title" => "音乐地址：");
	$media_music_t =
	array(
		"name" => "m_title",
		"type" => "text",
		"size" => "18",
		"title" => "专辑标题：");
	$media_music_a =
	array(
		"name" => "m_author",
		"type" => "text",
		"size" => "18",
		"title" => "专辑作者：");
	$media_music_y =
	array(
		"name" => "m_year",
		"type" => "text",
		"size" => "18",
		"title" => "专辑年份：");
	$media_video =
	array(
		"name" => "video",
		"type" => "text",
		"size" => "100",
		"title" => "视频地址：");
	$div1 = array( "type" => "html", "desc" => "<h3>媒体分享信息</h3>" );
	endif;
	$ini_post_meta = array(
		$cc, $top, $description, $keywords, $thumb, $div1, $media_music_t, $media_music_a, $media_music_y, $media_music, $media_video, $pgmenu
	);
	$out_put = filesize(dirname(__FILE__).'/rate.php');
	$out_pot = filesize(dirname(__FILE__).'/option.php');
	$_urlparts = parse_url(site_url());
	$foot_doma = $_urlparts['host'];
	$_dp = explode(".", $foot_doma);
	$foot_doma = $_dp[count($_dp)-2]. "." .$_dp[count($_dp)-1];
	function ini_copyright(){
	global $wpdb, $foot_doma;
	$copyright_dates = $wpdb->get_results("SELECT YEAR( min(post_date_gmt) ) AS firstdate, YEAR( max(post_date_gmt) ) AS lastdate FROM $wpdb->posts WHERE post_status = 'publish'");
	if( $copyright_dates ){
		$copyright = "Copyright &copy; ".$copyright_dates[0]->firstdate;
		if( $copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate ){
			$copyright.='-'.$copyright_dates[0]->lastdate;
		}
	}
	echo $copyright;
	echo "<a href='".get_bloginfo('url')."'> {$foot_doma}</a>.&nbsp;";
}

function ini_post_meta(){
	?><div id="meta-box"><?php
	global $post, $ini_post_meta;
	foreach($ini_post_meta as $meta_box){
		$meta_box_value = get_post_meta($post->ID, $meta_box['name'], true);
		if($meta_box_value == "") $meta_box_value = $meta_box['std'];
		switch ( $meta_box['type'] ){

case "text":
?>
<label><?php echo $meta_box['title']?>
<?php if( $meta_box['desc'] ) echo '<span class="desc">' .$meta_box['desc']. '</span><br>'?>
<input name="<?php echo $meta_box['name']; ?>" type="text" value="<?php echo $meta_box_value; ?>" size="<?php echo $meta_box['size']; ?>" />
</label>
<?php
break;

case "checkbox":
echo '<label>' .$meta_box['title']. '<input name="' .$meta_box['name']. '" type="checkbox" ';
echo $meta_box_value != '' ? 'checked=checked' : '';
echo '/></label>';
break;

case "textarea":
?>
<p><?php echo $meta_box['title']?>
<span class="desc"><?php echo $meta_box['desc'];?></span><br>
<textarea name="<?php echo $meta_box['name']; ?>" type="textarea"><?php echo $meta_box_value;?></textarea>
</p>
<?php
break;

case 'select':
?>
<label><?php echo $meta_box['title']?>
<select name="<?php echo $meta_box['name']; ?>">
<?php if( !$meta_box_value ) $meta_box_value = $meta_box['default']; foreach ( $meta_box['options'] as $option ){ $option = explode( "|" , $option )?>
	<option value="<?php echo $option[0]?>" <?php if( $meta_box_value == $option[0] ) echo 'selected="selected"'?> ><?php echo $option[1]?></option>
<?php }?>
</select>
</label>
<?php
break;

case "hidden":
echo '<input name="' .$meta_box['name']. '" type="hidden" value="' .$meta_box_value. '"/>';
break;

case "html":
echo $meta_box['desc'];
break;

		}
	}
	echo '<input type="hidden" name="newmetaboxes" id="newmetaboxes" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" /></div>';
}

function ini_post_meta_box(){
	global $theme;
	if ( function_exists('add_meta_box') ){
		add_meta_box( 'post-meta-boxes',  $theme .'自定义栏目', 'ini_post_meta', 'post', 'normal', 'high' );
	}
}

function ini_postdata( $post_id ){
	global $ini_post_meta;
	if ( !wp_verify_nonce( $_POST['newmetaboxes'], plugin_basename(__FILE__) ))
		return;
	if ( !current_user_can( 'edit_posts', $post_id ) || get('ini_mark'))
		return;
	foreach($ini_post_meta as $meta_box){
		$data = $_POST[$meta_box['name']];
		if(get_post_meta($post_id, $meta_box['name']) == "")
			add_post_meta($post_id, $meta_box['name'], $data, true);
		elseif($data != get_post_meta($post_id, $meta_box['name'], true))
			update_post_meta($post_id, $meta_box['name'], $data);
		elseif($data == "")
			delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
	}
}
add_action('admin_menu', 'ini_post_meta_box');
add_action('save_post', 'ini_postdata');




/* 页面自定义栏目表单 */

function ini_page_meta(){
	global $post;
?>
<div id="meta-box">
	文章描述<span class="desc">（默认截取前200字符）</span><br>
	<textarea name="description" type="textarea"><?php echo get_post_meta($post->ID, 'description', true)?></textarea><br>
	关键词<span class="desc">（半角逗号隔开，默认使用标签）</span><br>
	<textarea name="keywords" type="textarea"><?php echo get_post_meta($post->ID, 'keywords', true)?></textarea><br>
	<label>不显示页面导航：<input name="pgmenu" type="checkbox" <?php echo get_post_meta($post->ID, 'pgmenu', true) ? 'checked="checked"' : ''?>/></label>
	<input type="hidden" name="newmetaboxes" id="newmetaboxes" value="<?php echo wp_create_nonce( plugin_basename(__FILE__) )?>">
</div>
<?php
}

function ini_page_meta_box(){
	global $theme;
	if ( function_exists('add_meta_box') ){
		add_meta_box( 'page-meta-boxes', $theme .'自定义栏目', 'ini_page_meta', 'page', 'normal', 'high' );
	}
}

add_action('admin_menu', 'ini_page_meta_box');
